home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 3
/
Info_Mac_1994-01.iso
/
Science
/
MathPad 2.1.5
/
examples
/
curve fit
< prev
next >
Wrap
Text File
|
1993-06-21
|
1KB
|
39 lines
----------general least squares fit------
-- finds coefficients a[i] for best fit of weighted sum of functions of x
fit(x) = sum(a[i,1]*f(x)[i],i,1,nparms)
f(x)[j]=x^(j-1) -- polynomial a1+a2*x+a3*x^2...
P[i,j]=f(x[i])[j] dim[ndata,nparms]
PtP:=multiply(transpose(P),P):
Pty:=multiply(transpose(P),y):
inv:=invert(PtP):
a:=multiply(inv,Pty):
data=read(xydata)
x[i]=data[i,1]; y[i]=data[i,2] dim[ndata];
ndata=count(data)
nparms=3
a:{{5.582},{0.159},{0.006}}
plot data
plot fit(X)
---------- matrix operations ------------
multiply(A,B)[i,j] = sum(A[i,k]*B[k,j],k,
1,count(B)) dim[count(A),count(B[1])]
transpose(A)[i,j] = A[j,i] dim
[count(A[1]),count(A)]
invert(A) = adjoint(A)/det(A)
adjoint(A) = transpose(cofactor(A))
cofactor(A)[i,j] = (-1)^(i+j)*
det(submatrix(A,i,j))
submatrix(A,k,l)[i,j] = A[i,j] when i<k and j<l,
A[i+1,j] when i≥k and j<l,
A[i+1,j+1] when i≥k and j≥l,
A[i, j+1] when i<k and j≥l dim
[count(A)-1,count(A)-1]